package org.marverse.serialization

import java.nio.ByteBuffer
import scala.collection.mutable.Buffer

trait ObjectOutput {
  @throws(classOf[java.io.IOException])
  def writeBytes(buffer: ByteBuffer)
  @throws(classOf[java.io.IOException])
  def writeBoolean(value: Boolean)
  @throws(classOf[java.io.IOException])
  def writeChar(value: Char)
  @throws(classOf[java.io.IOException])
  def writeInt(value: Int)
  @throws(classOf[java.io.IOException])
  def writeLong(value: Long)
  @throws(classOf[java.io.IOException])
  def writeDouble(value: Double)
  @throws(classOf[java.io.IOException])
  def write(value: AnyRef)
  
  @throws(classOf[java.io.IOException])
  def writeCollection(collection: Collection[_ <: AnyRef]) {
    writeInt(collection.size)
    collection foreach write _
  }

  @throws(classOf[java.io.IOException])
  def writeMap(map: Map[_ <: AnyRef, _ <: AnyRef]) {
    writeInt(map.size)
    map foreach (x => { write(x._1); write(x._2) })
  }
}

trait InstanceSerializer[T <: AnyRef] {
  @throws(classOf[java.io.IOException])
  def serializeInstance(that: T, out: ObjectOutput)
}

trait Serializer[T <: AnyRef] {
  @throws(classOf[java.io.IOException])
  def serialize(that: T, out: ObjectOutput)
}